﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AlgoTester
{
    class Stockmarket
    {
        // Data Members
        private string name;
        private Date day;
        private SortedList<string, Stock> stockList;

        // Constructor
        public Stockmarket(string name, Date day)
        {
            this.name = name;
            this.day = day;
            this.stockList = new SortedList<string, Stock>();
            buildStocks(new FileOperator());
        }

        // Main Simulation Driver
        public void nextDay(Date day)
        {
            this.day = day;
        }

        // Retrieves Stock associated with arg ticker
        // Returns null if not found
        public Stock findStock(string key)
        {
            Stock returnStock = null;
            stockList.TryGetValue(key, out returnStock);
            return returnStock;
        }

        // Return tickers of all stocks in market
        public String[] getTickers()
        {
            IList<string> tickers = stockList.Keys;
            int len = tickers.Count;
            string[] returnArray = new string[len];
            for (int i = 0; i < len; i++)
            {
                returnArray[i] = tickers[i];
            }
            return returnArray;
        }


        // Call to automatically add stocks to market
        private void buildStocks(FileOperator fileop)
        {
            List<String[]> stockNameInfo = fileop.getStockList(name);
            // Constructs and adds each stock
            foreach (String[] stockInfo in stockNameInfo)
            {
                stockList.Add(stockInfo[1], new Stock(stockInfo[0], stockInfo[1], fileop.getStockInfo(stockInfo[1])));
            }
        }

    }
}
